<?php
/**
 * Created by PhpStorm.
 * User: Wenda Fang
 * Date: 14-1-4
 * Time: 下午1:30
 */
class database {
    private static $hostname = 'localhost';
    private static $user = 'root';
    private static $pwd = '';
    private static $db = 'studyking';
    public  $mysqli;
    function __construct() {
        $this->mysqli = new mysqli(self::$hostname, self::$user,self::$pwd,self::$db);
        /* check connection */
        if ($this->mysqli->connect_errno) {
            printf("Connect failed: %s\n", $this->mysqli->connect_error);
            exit();
        }
		$this->mysqli->query("set character set 'utf8'");
        $this->mysqli->query("set names 'utf8'");
    }
    public function get_siteinfo($id_category) {
        $query = 'select * from details, category_details where details.id = category_details.id_details'.
            ' and id_category = '.$id_category.' order by rate desc';
        if ( $this->mysqli->query($query)) {
            $result =  $this->mysqli->query($query);
        } else {
            printf("无法查询表".$this->mysqli->error);
        }
        return $result;
    }

    public function get_category($id) {
        $query = 'select name from category where id = '.$id;
        if ( $this->mysqli->query($query)) {
            $result =  $this->mysqli->query($query);
        } else {
            printf("无法查询表".$this->mysqli->error);
        }
        return $result;
    }

    public function add_site($siteInfo) {
        $query = 'insert into details value ('.$siteInfo['name'].','.$siteInfo['description'].
            ','.$siteInfo['link'].','.$siteInfo['photo'].','.$siteInfo['rate'];
        $result = $this->mysqli->query($query);
        return $result;
    }

    public function del($table,$id) {
        $query = 'delete * from'. $table.'where id='.$id;
        $result = $this->mysqli->query($query);
        return $result;
    }

    public function update($table,$id,$key,$value) {
        $query = 'update '.$table.' set '.$key.' = '.$value.' where id = '.$id;
        $result = $this->mysqli->query($query);
        return $result;
    }
    public function free_result($result) {
        $result->close();
    }
    public  function close() {
        $this->mysqli->close();
    }
}